package cn.iune.web.erp.controllers;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfWriter;
import com.justep.baas.Utils;

import cn.iune.config.SecurityService;
import cn.iune.erp.invoice.IAccountInvoiceService;
import cn.iune.erp.invoice.IInvoiceService;
import cn.iune.erp.invoice.IOtherTypeService;



@Controller
@RequestMapping(value="/invoice")
public class InvoiceController {
	
	@Autowired
	private IAccountInvoiceService accountInvoiceService;
	@Autowired
	private IInvoiceService invoiceService;
	
	@Autowired
	private IOtherTypeService otherTypeService;
	
	
	
	@RequestMapping(value= {
			"/receiveOtherType/list"
			},method=RequestMethod.GET)
	public ModelAndView receiveOtherTypeList(){
		ModelAndView mav = new ModelAndView("/appcenter/invoice/receiveOtherType/list");
		return mav;
	}

	@RequestMapping(value="/receiveOtherType/single",method=RequestMethod.GET)
	public ModelAndView receiveOtherTypeSingle(){
	
		ModelAndView mav = new ModelAndView("/appcenter/invoice/receiveOtherType/single");
		return mav;
	}
	
	@RequestMapping(value= {
			"/payableOtherType/list"
			},method=RequestMethod.GET)
	public ModelAndView payableOtherTypeList(){
		ModelAndView mav = new ModelAndView("/appcenter/invoice/payableOtherType/list");
		return mav;
	}

	@RequestMapping(value="/payableOtherType/single",method=RequestMethod.GET)
	public ModelAndView payableOtherTypeSingle(){
	
		ModelAndView mav = new ModelAndView("/appcenter/invoice/payableOtherType/single");
		return mav;
	}
	
	
	
	
	@RequestMapping(value={
			"/receiveOtherType/list/create"
			,"/payableOtherType/list/create"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject otherTypeCreate(@RequestBody JSONObject data) {
		data = SecurityService.translateFun(data);
		JSONObject json = otherTypeService.newData(data);
		return json;
	}

	@RequestMapping(value={
			"/receiveOtherType/list/query"
			,"/payableOtherType/list/query"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject otherTypeQuery(@RequestBody JSONObject data) {
		data = SecurityService.addDataAccessPerm(data);
		return otherTypeService.queryData(data);
	}

	@RequestMapping(value={
			"/receiveOtherType/list/save"
			,"/payableOtherType/list/save"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject otherTypeSave(@RequestBody JSONObject object) {
		return otherTypeService.saveData(object);
	}

	@RequestMapping(value={
			"/receiveOtherType/list/delete"
			,"/payableOtherType/list/delete"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject otherTypeDelete(@RequestBody JSONObject data) {
		return otherTypeService.deleteData(data);
	}
	
	

	@RequestMapping(value="/receiveOtherType/selectSingle",method=RequestMethod.GET)
	public ModelAndView receiveOtherTypeSelectSingle(){
	
		ModelAndView mav = new ModelAndView("/appcenter/invoice/receiveOtherType/selectSingle");
		return mav;
	}

	@RequestMapping(value="/payableOtherType/selectSingle",method=RequestMethod.GET)
	public ModelAndView payableOtherTypeSelectSingle(){
	
		ModelAndView mav = new ModelAndView("/appcenter/invoice/payableOtherType/selectSingle");
		return mav;
	}

	
	//选择应收应付实收实付
	@RequestMapping(value="/invoiceSelect/selectMulti",method=RequestMethod.GET)
	public ModelAndView invoiceSelectMultip(){
		ModelAndView mav = new ModelAndView("/appcenter/invoice/invoiceSelect/selectMulti");
		return mav;
	}

	
	//采购发票
	@RequestMapping(value="/payableCGFP/single",method=RequestMethod.GET)
	public ModelAndView payableCGFPSingle(){
		ModelAndView mav = new ModelAndView("/appcenter/invoice/payableCGFP/single");
		mav.addObject("path","/invoice/payableCGFP/single");
		return mav;
	}
	
	//其他应付
	@RequestMapping(value="/payableQTYF/single",method=RequestMethod.GET)
	public ModelAndView payableQTYFSingle(){
		ModelAndView mav = new ModelAndView("/appcenter/invoice/payableQTYF/single");
		mav.addObject("path","/invoice/payableQTYF/single");
		return mav;
	}
	
	//销售发票
	@RequestMapping(value="/receiveXSFP/single",method=RequestMethod.GET)
	public ModelAndView receiveXSFPSingle(){
		ModelAndView mav = new ModelAndView("/appcenter/invoice/receiveXSFP/single");
		mav.addObject("path","/invoice/receiveXSFP/single");
		return mav;
	}
	
	//其他应收单
	@RequestMapping(value="/receiveQTYS/single",method=RequestMethod.GET)
	public ModelAndView receiveQTYSSingle(){
		ModelAndView mav = new ModelAndView("/appcenter/invoice/receiveQTYS/single");
		mav.addObject("path","/invoice/receiveQTYS/single");
		return mav;
	}
	
	
	
	
	@RequestMapping(value={
			"/payableCGFP/single/query"
			,"/payableQTYF/single/query"
			,"/receiveXSFP/single/query"
			,"/receiveQTYS/single/query"
//			,"/payableDJQA/single/query"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject payableCGFPQuery(@RequestBody JSONObject data) {
		data = SecurityService.addDataAccessPerm(data);
		return accountInvoiceService.queryData(data);
	}

	@RequestMapping(value={
			"/payableCGFP/single/save"
			,"/payableQTYF/single/save"
			,"/receiveXSFP/single/save"
			,"/receiveQTYS/single/save"
//			,"/payableDJQA/single/save"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject payableCGFPSave(@RequestBody JSONObject object) {
		return accountInvoiceService.saveData(object);
	}

	@RequestMapping(value={
			"/payableCGFP/single/delete"
			,"/payableQTYF/single/delete"
			,"/receiveXSFP/single/delete"
			,"/receiveQTYS/single/delete"
//			,"/payableDJQA/single/delete"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject payableCGFPDelete(@RequestBody JSONObject data) {
		return accountInvoiceService.deleteData(data);
	}

	@RequestMapping(value={
			"/payableCGFP/single/create"
			,"/payableQTYF/single/create"
			,"/receiveXSFP/single/create"
			,"/receiveQTYS/single/create"
//			,"/payableDJQA/single/create"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject payableCGFPCreate(@RequestBody JSONObject data, HttpServletRequest request) {
		data = SecurityService.translateFun(data);
		JSONObject json = accountInvoiceService.newData(data);
		return json;
	}


	@RequestMapping(value={
			"/payableCGFP/single/createDetail"
			,"/payableQTYF/single/createDetail"
			,"/receiveXSFP/single/createDetail"
			,"/receiveQTYS/single/createDetail"
//			,"/payableDJQA/single/createDetail"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject payableCGFPCreateDetail(@RequestBody JSONObject data) {
		JSONObject json = accountInvoiceService.newDataDetail(data);
		return json;
	}

	@RequestMapping(value={
			"/payableCGFP/single/queryDetail"
			,"/payableQTYF/single/queryDetail"
			,"/receiveXSFP/single/queryDetail"
			,"/receiveQTYS/single/queryDetail"
//			,"/payableDJQA/single/queryDetail"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject payableCGFPQueryDetail(@RequestBody JSONObject data) {
		return accountInvoiceService.queryDataDetail(data);
	}

	

	
	//////////////////////////////////////////////////////////////////////////////////////
	@RequestMapping(value={
			"/payableCGFP/single/confirm"
			,"/payableQTYF/single/confirm"
			,"/receiveXSFP/single/confirm"
			,"/receiveQTYS/single/confirm"
//			,"/payableDJQA/single/confirm"
			},method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> payableCGFPConfirm(@RequestBody JSONObject data) {
		String id = data.getString("id");
		
		String tUsercode = SecurityService.getCurrentPerson().getString("cUsercode");
		String tUsername = SecurityService.getCurrentPerson().getString("cUsername");
		String userId = SecurityService.getCurrentPerson().getString("userId");
		
		String employeeNo = SecurityService.getCurrentPsm().getsFID();
		String employeeName = SecurityService.getCurrentPsm().getsFName();
		
		return invoiceService.accountInvoiceConfirm(id,tUsercode,tUsername,userId,employeeNo,employeeName);
	}

	@RequestMapping(value={
			"/payableCGFP/single/active"
			,"/payableQTYF/single/active"
			,"/receiveXSFP/single/active"
			,"/receiveQTYS/single/active"
//			,"/payableDJQA/single/active"
			},method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> payableCGFPActive(@RequestBody JSONObject data) {
		String id = data.getString("id");
		return invoiceService.accountInvoiceActive(id);
	}
	@RequestMapping(value={
			"/payableCGFP/single/agreeIt"
			,"/payableQTYF/single/agreeIt"
			,"/receiveXSFP/single/agreeIt"
			,"/receiveQTYS/single/agreeIt"
//			,"/payableDJQA/single/agreeIt"
			},method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> payableCGFPAgreeIt(@RequestBody JSONObject data) {
		String id = data.getString("id");
		
		String tUsercode = SecurityService.getCurrentPerson().getString("cUsercode");
		String tUsername = SecurityService.getCurrentPerson().getString("cUsername");
		String userId = SecurityService.getCurrentPerson().getString("userId");
		
		String employeeNo = SecurityService.getCurrentPsm().getsFID();
		String employeeName = SecurityService.getCurrentPsm().getsFName();
		
		return invoiceService.accountInvoiceAgreeIt(id,tUsercode,tUsername,userId,employeeNo,employeeName);
	}

	@RequestMapping(value={
			"/payableCGFP/single/unAgree"
			,"/payableQTYF/single/unAgree"
			,"/receiveXSFP/single/unAgree"
			,"/receiveQTYS/single/unAgree"
//			,"/payableDJQA/single/unAgree"
			},method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> payableCGFPUnAgree(@RequestBody JSONObject data) {
		String id = data.getString("id");
		return invoiceService.accountInvoiceUnAgree(id);
	}
	
	@RequestMapping(value={
			"/payableCGFP/single/close"
			,"/payableQTYF/single/close"
			,"/receiveXSFP/single/close"
			,"/receiveQTYS/single/close"
//			,"/payableDJQA/single/close"
			},method=RequestMethod.POST)
	@ResponseBody
	public void payableCGFPClose(@RequestBody JSONObject data) {
		String id = data.getString("id");
		invoiceService.accountInvoiceClose(id);
	}

	@RequestMapping(value={
			"/payableCGFP/single/open"
			,"/payableQTYF/single/open"
			,"/receiveXSFP/single/open"
			,"/receiveQTYS/single/open"
//			,"/payableDJQA/single/open"
			},method=RequestMethod.POST)
	@ResponseBody
	public void payableCGFPOpen(@RequestBody JSONObject data) {
		String id = data.getString("id");
		invoiceService.accountInvoiceOpen(id);
	}
	

	//发票打印
	@RequestMapping(value={
			"/payableCGFP/single/printPDF"
			,"/receiveXSFP/single/printPDF"
			},method=RequestMethod.GET)
	@ResponseBody
	public void payableCGFPPrintPDF(@RequestParam(value="id",required=true) String id, HttpServletRequest request, HttpServletResponse response) throws Exception {
		//invoiceService.accountInvoicePrintPDF(id, response);
	    Document doc = new Document();// 创建一个PDF文档，我们的PDF内容都写在上面
	    response.setContentType("application/pdf");
//	    PdfWriter writer = PdfWriter.getInstance(doc, response.getOutputStream());
	    PdfWriter.getInstance(doc, response.getOutputStream());
	    //打开doc
	    doc.open();
	    //填写doc内容
	    invoiceService.accountInvoicePrintPDF(id,doc);
		//关闭doc
	    doc.close();
	}
	
	//其他应收应付打印
	@RequestMapping(value={
			"/payableQTYF/single/printPDF"
			,"/receiveQTYS/single/printPDF"
			},method=RequestMethod.GET)
	@ResponseBody
	public void payableQTYFPrintPDF(@RequestParam(value="id",required=true) String id, HttpServletRequest request, HttpServletResponse response) throws Exception {
		//invoiceService.accountInvoiceQTYFPrintPDF(id, response);
	    Document doc = new Document();// 创建一个PDF文档，我们的PDF内容都写在上面
	    response.setContentType("application/pdf");
//	    PdfWriter writer = PdfWriter.getInstance(doc, response.getOutputStream());
	    PdfWriter.getInstance(doc, response.getOutputStream());
	    //打开doc
	    doc.open();
	    //填写doc内容
	    invoiceService.accountInvoiceQTYFPrintPDF(id,doc);
		//关闭doc
	    doc.close();
	}

	
	//采购发票明细查询
	@RequestMapping(value="/payableCGFPDetailQuery/list",method=RequestMethod.GET)
	public ModelAndView payableCGFPDetailQuery(){
		ModelAndView mav = new ModelAndView("/appcenter/invoice/payableCGFPDetailQuery/list");
		return mav;
	}
	
	//销售发票明细查询
	@RequestMapping(value="/receiveXSFPDetailQuery/list",method=RequestMethod.GET)
	public ModelAndView receiveXSFPDetailQuery(){
		ModelAndView mav = new ModelAndView("/appcenter/invoice/receiveXSFPDetailQuery/list");
		return mav;
	}
	
	/*
	//由销售发货单生成销售发票（合并收款）
	@RequestMapping(value = "/fillinXSFPBuild", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject fillinXSFPBuild(@RequestBody JSONObject params, HttpSession session){

		//填写付款单付款
		AccountInvoice obj = invoiceService.accountInvoiceXSFPBuild(params);
		MapMessage mapMessage = null;
		
		if(obj==null){
//			JSONObject json = new JSONObject();
//			json.put("code", -1);
//			json.put("status", "生成单据失败");
			
			mapMessage = new MapMessage(null,null,null,null
					,MapMessage.STATUSINFO_FAILURE_CODE//"http://www.iune.cn"
					,MapMessage.STATUSINFO_FAILURE_MSG//"操作成功！"
					,MapMessage.STATUSINFO_FAILURE_INFO//"操作成功！"
					,MapMessage.STATUSINFO_FAILURE_FLAG//"success"
					);			
			return mapMessage.toJSONObject();
		}
		
//		JSONObject json = new JSONObject();
//		json.put("code", 0);
//		json.put("status", "成功");
//		json.put("id", accountInvoice.getfID());
//		json.put("billNo", accountInvoice.getAccountInvoiceNo());
		
		mapMessage = new MapMessage(obj.getfID(),obj.getAccountInvoiceNo(),obj.getBillType(),obj.getBillTypeName()
				,MapMessage.STATUSINFO_SUCCESS_CODE//"http://www.iune.cn"
				,MapMessage.STATUSINFO_SUCCESS_MSG//"操作成功！"
				,MapMessage.STATUSINFO_SUCCESS_INFO//"操作成功！"
				,MapMessage.STATUSINFO_SUCCESS_FLAG//"success"
				);
		
		SysInfo sysInfo = sysInfoService.getByID(obj.getSystemCode());
		if(sysInfo!=null){
			mapMessage.setAppID(sysInfo.getfID());
			mapMessage.setsProcess(sysInfo.getAbsoluteUrl());
			mapMessage.setsName(sysInfo.getSystemName());
		}
		return mapMessage.toJSONObject();
		
	}
	*/
	
	
	//////////////////////////////////////////////////////////////////////////////////////
	

	//应付账款明细账查询
	@RequestMapping(value="/payableYFZKQuery/list",method=RequestMethod.GET)
	public ModelAndView payableYFZKQuery(){
		ModelAndView mav = new ModelAndView("/appcenter/invoice/payableYFZKQuery/list");
		return mav;
	}

	//应收账款明细账查询
	@RequestMapping(value="/receiveYSZKQuery/list",method=RequestMethod.GET)
	public ModelAndView receiveYSZKQuery(){
		ModelAndView mav = new ModelAndView("/appcenter/invoice/receiveYSZKQuery/list");
		return mav;
	}
	
	
	
	@RequestMapping(value={
			"/accountInvoice/query"
			,"/payableYFZKQuery/list/query"
			,"/receiveYSZKQuery/list/query"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject accountInvoiceQuery(@RequestBody JSONObject data) {
		data = SecurityService.addDataAccessPerm(data);
		return accountInvoiceService.queryData(data);
	}
	
	@RequestMapping(value={"/accountInvoiceDetail/query"},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject accountInvoiceDetailQuery(@RequestBody JSONObject data) {
		data = SecurityService.addDataAccessPerm(data);
		return accountInvoiceService.queryDataDetail(data);
	}
	

	//应收账款金额
	@RequestMapping(value="/receiveYSZKQuery/getReceiveYSZKAmount",method=RequestMethod.GET)
	@ResponseBody
	public JSONObject getReceiveYSZKAmount() {
		JSONObject json = new JSONObject();
		
		if(SecurityService.getCurrentOgn()==null){
			//实际上下面这一段不会被执行，因为shiro会首先拦截掉未登录的请求，重定向到未授权页面。
			json.put("code", 403);
			json.put("msg", "未登录");
			return json;
			
		}else {
			String enterpriseNo = SecurityService.getCurrentOgn().getsFID();
			List<Object> params = new ArrayList<Object>();
			params.add(enterpriseNo);
			params.add(Utils.getCurrentDate());
			BigDecimal count = accountInvoiceService.getReceiveAmountTotay(params);
			if(count==null) count = BigDecimal.ZERO;
			json.put("code", 0);
			json.put("msg", "success");
			json.put("data", count);
			return json;
		}
	}
	

	//应付账款金额
	@RequestMapping(value="/payableYFZKQuery/getPayableYFZKAmount",method=RequestMethod.GET)
	@ResponseBody
	public JSONObject getPayableYFZKAmount() {
		JSONObject json = new JSONObject();
		
		if(SecurityService.getCurrentOgn()==null){
			//实际上下面这一段不会被执行，因为shiro会首先拦截掉未登录的请求，重定向到未授权页面。
			json.put("code", 403);
			json.put("msg", "未登录");
			return json;
			
		}else {
			String enterpriseNo = SecurityService.getCurrentOgn().getsFID();
			List<Object> params = new ArrayList<Object>();
			params.add(enterpriseNo);
			params.add(Utils.getCurrentDate());
			BigDecimal count = accountInvoiceService.getPayableAmountTotay(params);
			if(count==null) count = BigDecimal.ZERO;
			json.put("code", 0);
			json.put("msg", "success");
			json.put("data", count);
			return json;
		}
	}
	
	//合并结算（生成销售发票）
	@RequestMapping(value="/receiveXSFPBuild/list",method=RequestMethod.GET)
	public ModelAndView receiveXSFPBuildList(){
		ModelAndView mav = new ModelAndView("/appcenter/invoice/receiveXSFPBuild/list");
		return mav;
	}

	
	//合并收款（生成收款单）
	@RequestMapping(value="/recSKDJBuild/list",method=RequestMethod.GET)
	public ModelAndView recSKDJBuild(){
		ModelAndView mav = new ModelAndView("/appcenter/invoice/recSKDJBuild/list");
		return mav;
	}

	@RequestMapping(value={
			"/recSKDJBuild/query"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject recSKDJBuildQuery(@RequestBody JSONObject data) {
		return accountInvoiceService.queryData(data);
	}


	
	
	//////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////

	
}
